package cre.spring.mvc.web;

import cre.spring.mvc.custom.DemoDateEditor;
import cre.spring.mvc.domain.Demo;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author penglai
 * @Date 2017/4/5.
 */
@Controller
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private ServletContext servletContext;

    @InitBinder
    public void initBinder(PropertyEditorRegistry registry) {
        registry.registerCustomEditor(Date.class, new DemoDateEditor(new SimpleDateFormat("yyyy-HH-dd"), true));
    }

    @RequestMapping("index")
    @ResponseBody
    public String index() {
        return "index";
    }

    @RequestMapping("m1")
    public String m1(String name, ModelMap modelMap, Date date) {
        modelMap.put("name", "val");
        System.out.println("in m1");
        return "m1";
    }

    @RequestMapping("m2")
    public String m2(Demo demo) {
        System.out.println("in m2");
        return "m2";
    }

    @RequestMapping("/test/**")
    @ResponseBody
    public String test1(ModelAndView view) {
        view.setViewName("/test/test");
        view.addObject("attr", "TestWildcardController -> /test/**");
        return view.getViewName();
    }

    @RequestMapping("/test/*")
    @ResponseBody
    public String test2(ModelAndView view) {
        view.setViewName("/test/test");
        view.addObject("attr", "TestWildcardController -> /test*");
        return view.getViewName();
    }

    @RequestMapping("test?")
    @ResponseBody
    public String test3(ModelAndView view) {
        view.setViewName("/test/test");
        view.addObject("attr", "TestWildcardController -> test?");
        return view.getViewName();
    }

    @RequestMapping("test/download")
    public String download(HttpServletRequest request, HttpServletResponse response) {
        try {
            System.out.println(servletContext.getRealPath("")+"f");
            System.out.println(servletContext.getRealPath("")+"/f");
            System.out.println(servletContext.getRealPath("f/"));
            System.out.println(servletContext.getRealPath("f//"));
            System.out.println(servletContext.getRealPath("f/1/"));
            System.out.println(servletContext.getRealPath("f/1/2/3"));
            System.out.println(servletContext.getRealPath("file"));
            System.out.println(servletContext.getRealPath("/file"));
            System.out.println(servletContext.getRealPath("/fi"));

            String filePath = servletContext.getRealPath("file") + "/文件下载-2017-11-08.txt";
            File file = new File(filePath);
            FileInputStream inputStream = new FileInputStream(file);
            String filename = file.getName();
            filename = URLEncoder.encode(filename, "UTF-8");
            //set header
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            response.setHeader("Content-Length", String.valueOf(inputStream.available()));

            //set io

            IOUtils.copy(inputStream, response.getOutputStream());
            response.getOutputStream().flush();
            response.flushBuffer();
            IOUtils.closeQuietly(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    String gainContentType(String ext) {
        String[] doc = { "doc", "docx" };
        String[] excel = { "xls", "xlsx" };
        String[] jpg = { "jpg", "jpeg" };
        String[] txt = { "txt" };
        if (ArrayUtils.contains(doc, ext)) {
            return "application/msword";
        } else if (ArrayUtils.contains(excel, ext)) {
            return "application/vnd.ms-excel";
        } else if (ArrayUtils.contains(jpg, ext)) {
            return "image/jpeg";
        } else if (ArrayUtils.contains(txt, ext)) {
            return "text/plain";
        }
        return "application/octet-stream";
    }
}
